library(haven)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
categ <- read_dta(file = "ByCateg_v5.dta")
categfull <- categ %>%
filter(!is.na(price))
Data Visualizations
ggplot(data = categfull, mapping = aes(x = price, y = densprot)) +
geom_point(aes(color = broadcateg_name, size = tspend),
alpha = 0.5) +
labs(title = "Protein on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
scale_size(guide = 'none') +
scale_color_discrete(name = "Food Category")

prot <- plot_ly(
categfull, x = ~price, y = ~densprot,
# Hover text:
text = ~paste("<b>Broad Category:</b>", broadcateg_name, "<br><b>Type:</b>", categ_name, "<br><b>Description:</b>", categ_des),
color = ~broadcateg_name, size = ~tspend, opacity = 0.8
)
prot %>%
layout(title = "Protein",
xaxis = list(title = "Price/1000kcal"), yaxis = list(title = "Protein/1000kcal"),
margin = list(l = 123))
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
ggplot(data = categfull, mapping = aes(x = price, y = denssodi)) +
geom_point(aes(color = broadcateg_name, size = tspend),
alpha = 0.5) +
labs(title = "Sodium on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
scale_size(guide = 'none') +
scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = dens_add_sugars)) +
geom_point(aes(color = broadcateg_name, size = tspend),
alpha = 0.5) +
labs(title = "Sugar on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
scale_size(guide = 'none') +
scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = densfibe)) +
geom_point(aes(color = broadcateg_name, size = tspend),
alpha = 0.5) +
labs(title = "Fiber on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
scale_size(guide = 'none') +
scale_color_discrete(name = "Food Category")
